"""
余额相关的Pydantic模式
"""

from pydantic import BaseModel, Field, validator
from typing import Optional, List
from datetime import datetime
from decimal import Decimal
from enum import Enum


class TransactionTypeSchema(str, Enum):
    """交易类型枚举"""
    REGISTER_BONUS = "register_bonus"
    RECHARGE = "recharge"
    CONSUME = "consume"
    REFUND = "refund"
    ADMIN_ADJUST = "admin_adjust"


class BalanceResponse(BaseModel):
    """余额响应模式"""
    id: int = Field(..., description="余额记录ID")
    user_id: int = Field(..., description="用户ID")
    amount: Decimal = Field(..., description="当前余额")
    frozen_amount: Decimal = Field(..., description="冻结余额")
    available_amount: Decimal = Field(..., description="可用余额")
    total_recharged: Decimal = Field(..., description="累计充值")
    total_consumed: Decimal = Field(..., description="累计消费")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    
    class Config:
        from_attributes = True


class BalanceTransactionCreate(BaseModel):
    """余额交易创建模式"""
    transaction_type: TransactionTypeSchema = Field(..., description="交易类型")
    amount: Decimal = Field(..., gt=0, description="交易金额")
    description: Optional[str] = Field(None, max_length=500, description="交易描述")
    reference_id: Optional[str] = Field(None, max_length=100, description="关联业务ID")
    reference_type: Optional[str] = Field(None, max_length=50, description="关联业务类型")


class BalanceTransactionResponse(BaseModel):
    """余额交易响应模式"""
    id: int = Field(..., description="交易记录ID")
    user_id: int = Field(..., description="用户ID")
    transaction_type: TransactionTypeSchema = Field(..., description="交易类型")
    amount: Decimal = Field(..., description="交易金额")
    balance_before: Decimal = Field(..., description="交易前余额")
    balance_after: Decimal = Field(..., description="交易后余额")
    description: Optional[str] = Field(None, description="交易描述")
    reference_id: Optional[str] = Field(None, description="关联业务ID")
    reference_type: Optional[str] = Field(None, description="关联业务类型")
    external_transaction_id: Optional[str] = Field(None, description="外部交易ID")
    payment_method: Optional[str] = Field(None, description="支付方式")
    created_at: datetime = Field(..., description="创建时间")
    
    class Config:
        from_attributes = True


class RechargeRequest(BaseModel):
    """充值请求模式"""
    amount: Decimal = Field(..., gt=0, le=10000, description="充值金额")
    payment_method: str = Field(..., description="支付方式")
    return_url: Optional[str] = Field(None, description="支付成功回调URL")
    
    @validator('amount')
    def validate_amount(cls, v):
        if v < Decimal('1.00'):
            raise ValueError('充值金额不能少于1元')
        if v > Decimal('10000.00'):
            raise ValueError('单次充值金额不能超过10000元')
        return v


class RechargeResponse(BaseModel):
    """充值响应模式"""
    transaction_id: str = Field(..., description="交易ID")
    amount: Decimal = Field(..., description="充值金额")
    payment_method: str = Field(..., description="支付方式")
    payment_url: Optional[str] = Field(None, description="支付链接")
    qr_code: Optional[str] = Field(None, description="支付二维码")
    expires_at: datetime = Field(..., description="支付过期时间")
    status: str = Field(..., description="支付状态")


class BalanceStatistics(BaseModel):
    """余额统计模式"""
    total_users: int = Field(..., description="总用户数")
    total_balance: Decimal = Field(..., description="总余额")
    total_recharged: Decimal = Field(..., description="总充值金额")
    total_consumed: Decimal = Field(..., description="总消费金额")
    avg_balance: Decimal = Field(..., description="平均余额")
    active_users: int = Field(..., description="活跃用户数")
    
    # 今日统计
    today_recharge: Decimal = Field(..., description="今日充值")
    today_consume: Decimal = Field(..., description="今日消费")
    today_new_users: int = Field(..., description="今日新增用户")


class TransactionSummary(BaseModel):
    """交易汇总模式"""
    user_id: int = Field(..., description="用户ID")
    total_transactions: int = Field(..., description="总交易数")
    total_recharge: Decimal = Field(..., description="总充值")
    total_consume: Decimal = Field(..., description="总消费")
    last_transaction_at: Optional[datetime] = Field(None, description="最后交易时间")
    
    # 按类型统计
    register_bonus: Decimal = Field(..., description="注册奖励")
    recharge_amount: Decimal = Field(..., description="充值金额")
    consume_amount: Decimal = Field(..., description="消费金额")
    refund_amount: Decimal = Field(..., description="退款金额")
